import abc
import time

import requests

import coin.exchange.base.kr_rest.rest_client_base as rcb


class NativePublicClientBase(rcb.RestClientBase):
  __metaclass__ = abc.ABCMeta

  def __init__(self, *, url, timeout=3):
    self.url = url
    self.timeout = timeout

  def _get(self, path, params=None):
    req_timestamp = time.time()
    full_url = "%s/%s" % (self.url, path)
    r = requests.get(full_url, params=params, timeout=self.timeout)
    if r.status_code == requests.codes.ok:
      res_timestamp = time.time()
      return rcb.RestUpdate(req_timestamp, res_timestamp, r)
    else:
      res_timestamp = time.time()
      return rcb.RestUpdate(req_timestamp, res_timestamp, None, status=r.status_code)

  def _get_as_json(self, path, params=None, headers=None):
    req_timestamp = time.time()
    if path.startswith('http'):
      full_url = path
    else:
      full_url = "%s/%s" % (self.url, path)
    r = requests.get(full_url, headers=headers, params=params, timeout=self.timeout)
    if r.status_code == requests.codes.ok:
      res_timestamp = time.time()
      return rcb.RestUpdate(req_timestamp, res_timestamp, r.json())
    else:
      res_timestamp = time.time()
      return rcb.RestUpdate(req_timestamp, res_timestamp, None, status=r.status_code)

  @abc.abstractmethod
  def queryAll(self):
    raise NotImplementedError()
